<template>
	<view class="userCenter_container">
		<!-- 用户信息资料 -->
		<view class="infoBox">
			<view class="infoCard">
				<!-- 个人资料信息 -->
				<view class="personCard">
					<view class="left">
						<view class="name">
							<text v-if="baseInfo.name">{{ baseInfo.name }}</text>
							<text v-else>-</text>
						</view>
						<view class="genderAndAge">
							<text class="icon icon1"></text>
							<text v-if="baseInfo.gender || baseInfo.age">
								<text v-if="baseInfo.gender == 0">不详·</text>
								<text v-if="baseInfo.gender == 1">男·</text>
								<text v-if="baseInfo.gender == 2">女·</text>
								<text>{{ baseInfo.age }}岁</text>
							</text>
							<text v-else>-</text>
						</view>
						<view class="birthday">
							<text class="icon icon2"></text>
							<text v-if="baseInfo.birthday">{{ baseInfo.birthday }}</text>
							<text v-else>-</text>
						</view>
						<view class="phoneNum">
							<text class="icon icon3"></text>
							<text v-if="baseInfo.phone">{{ baseInfo.phone }}</text>
							<text v-else>-</text>
						</view>
					</view>
					<view class="right">
						<view class="floatBox">
							<view class="avatar">
								<view class="box">
									<img src="@/static/userCenter/avatar.png">
								</view>
							</view>
							<view @click="showChangeInfoDialog" class="modifyInfo">
								<text class="icon"></text>
								<text class="text">修改资料</text>
							</view>
						</view>
					</view>
				</view>
				<!-- 个人身体信息 -->
				<view class="bodyCard">
					<view class="item">
						<view v-if="baseInfo.height" class="num">{{ baseInfo.height }}cm</view>
						<view v-else>-</view>
						<view class="text">身高</view>
					</view>
					<view class="item">
						<view v-if="baseInfo.weight" class="num">{{ baseInfo.weight }}kg</view>
						<view v-else>-</view>
						<view class="text">体重</view>
					</view>
					<view class="item">
						<view v-if="baseInfo.sleepingPosition" class="num">
							<text v-if="baseInfo.sleepingPosition == 1">平躺</text>
							<text v-if="baseInfo.sleepingPosition == 2">左侧卧</text>
							<text v-if="baseInfo.sleepingPosition == 3">右侧卧</text>
							<text v-if="baseInfo.sleepingPosition == 4">俯卧</text>
						</view>
						<view v-else>-</view>
						<view class="text">睡姿偏好</view>
					</view>
				</view>
			</view>
		</view>	
		<!-- 登录/退出登录 -->
		<view class="loginBox">
			<!-- 提示是否退出登录弹窗 -->
			<u-modal :show="logoutDialog" title="提示" :currentTitleStyle="currentTitleStyle" ref="uModal" class="submitSuccessDialog">
				<view class="slot-content" style="text-align: center; font-size: 28rpx;">
					<text>是否确认退出当前用户？</text>
				</view>
				<template slot="confirmButton" class="footer">
					<logoutFooter @confirmLogoutDialog="confirmLogoutDialog" @closeLogoutDialog="closeLogoutDialog" />
				</template>
			</u-modal>

			<!-- 修改信息提示弹窗 -->
			<u-modal :show="toChangeTipsDialog" title="提示" :currentTitleStyle="currentTitleStyle" ref="uModal1" class="changeInfoDialog">
				<view class="slot-content" style="text-align: center; font-size: 28rpx;">
					<view style="margin-bottom: 20rpx;">是否确认修改个人信息？</view>
					<view style="color: red;font-size: 24rpx;">*注意事项：修改基本信息后，已生成的报告将会失效</view>
				</view>
				<template slot="confirmButton" class="footer">
					<logoutFooter @confirmLogoutDialog="toModifyInfo" @closeLogoutDialog="closeChangeTipsDialog" />
				</template>
			</u-modal>

			<view v-if="token" class="box">
				<view class="tips">*注意事项：修改基本信息后，已生成的报告将会失效</view>
				<button @click="logout" class="logout">退出登录</button>
			</view>
			<view v-else class="box">
				<button @click="login" class="logon">登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapGetters } from 'vuex';
	import logoutFooter from "./components/logoutFooter.vue";
	import { removeLoginInfo } from '@/utils/auth';
	import { logout } from "@/api/login.js";
	import { getUserInfo } from '@/api/home.js';
	export default {
		components: {
			logoutFooter,
		},
		data() {
			return {
				logoutDialog: false,
				currentTitleStyle: `padding: 20rpx 0;background-color: #ededed;color: #000`,
				toChangeTipsDialog: false,
				baseInfo: {
					name: '',
					gender: '',
					age: '',
					phone: '',
					height: '',
					weight: '',
					BMI: '',
					birthday: '',
					sleepingPosition: '',
				}
			};
		},
		computed: {
			...mapGetters(['token'])
		},
		methods: {
			showChangeInfoDialog() {
				if (!this.token) {
					uni.navigateTo({
						url: "/pages/login/login",
					});
					return false;
				} else {
					this.toChangeTipsDialog = true
				}
			},
			toModifyInfo() {
				uni.navigateTo({
					url: `/subPages/baseInfo/baseInfo?isChangeBaseInfo=true`
				})
				this.toChangeTipsDialog = false;
			},
			logout() {
				this.logoutDialog = true;
			},
			login() {
				uni.navigateTo({
					url: '/pages/login/login'
				});
			},
			async confirmLogoutDialog() {
				let res = await logout();
				removeLoginInfo();
				this.$store.commit('user/resetUserInfo','');
				this.logoutDialog = false;
				uni.reLaunch({
					url: '/pages/login/login'
				});
			},
			closeLogoutDialog() {
				this.logoutDialog = false;
			},
			closeChangeTipsDialog() {
				this.toChangeTipsDialog = false;
			},
			async getUserInfo() {
				if (!this.token) return;
				let res = await getUserInfo();
				if (res.code == '1') {
					this.baseInfo.name = res.data.realName;
					this.baseInfo.gender = res.data.sex;
					this.baseInfo.phone = res.data.phoneNumber;
					this.baseInfo.birthday = res.data.birthday.slice(0, 10);

					// 获取年龄
					let birthYear = res.data.birthday.slice(0, 4);
					let nowYear = new Date().getFullYear();
					this.baseInfo.age = nowYear - birthYear;

					// 获取身高体重BMI睡姿偏好
					if (res.data.appMemberFieldInfoList && res.data.appMemberFieldInfoList.length) {
						res.data.appMemberFieldInfoList.forEach(item => {
							if (item.fieldKey == 'height') {
								this.baseInfo.height = item.fieldValue;
							}
							if (item.fieldKey == 'weight') {
								this.baseInfo.weight = item.fieldValue;
							}
							if (item.fieldKey == 'bmi') {
								this.baseInfo.BMI = item.fieldValue;
							}
							if (item.fieldKey == 'sleepPosition') {
								this.baseInfo.sleepingPosition = item.fieldValue - 0;
							}
						})
					}
				}
			},
		},
		onShow() {
			this.getUserInfo();
		}
	}
</script>

<style lang="scss">
.userCenter_container {
	display: flex;
	flex-direction: column;
	height: 100vh;
	overflow: hidden;

	.infoBox,.loginBox {
		flex: 1;
	}

	.infoBox {
		position: relative;
		background-image: url("~@/static/userCenter/infoBox_bg.png");
		background-size: 100% 100%;
		background-position: center -70rpx;
		background-repeat: no-repeat;

		.infoCard {
			position: absolute;
			left: 50%;
			bottom: 0;
			width: 90%;
			background-color: #fff;
			transform: translateX(-50%);
			border-radius: 20rpx;
			box-shadow: 0 0 20rpx 0 rgba(0,0,0,.2);

			.personCard {
				display: flex;
				padding: 30rpx 20rpx;
				border-bottom: 1rpx solid #e5e5e5;

				.left,.right {
					flex: 1;
				}

				.left {
					.name {
						height: 50rpx;
						line-height: 50rpx;
						font-size: 36rpx;
						font-weight: 700;
						margin-bottom: 10rpx;
					}

					.genderAndAge,.birthday,.phoneNum {
						display: flex;
						align-items: center;
						color: #9b9b9b;
						height: 44rpx;
						line-height: 44rpx;
						font-size: 28rpx;

						.icon {
							display: inline-block;
							width: 28rpx;
							height: 28rpx;
							margin-right: 15rpx;
						}

						.icon1 {
							background-image: url("~@/static/userCenter/userIcon.png");
							background-size: 100% 100%;
						}

						.icon2 {
							background-image: url("~@/static/userCenter/birthdayIcon.png");
							background-size: 100% 100%;
						}

						.icon3 {
							background-image: url("~@/static/userCenter/phoneIcon.png");
							background-size: 100% 100%;
						}
					}
				}

				.right {
					position: relative;

					.floatBox {
						position: absolute;
						right: 18rpx;
						top: -72rpx;
						width: 170rpx;

						.avatar {
							width: 150rpx;
							height: 150rpx;
							padding: 10rpx;
							border-radius: 20rpx;
							background-color: #fff;
							margin-bottom: 10rpx;

							.box {
								display: flex;
								justify-content: center;
								align-items: center;
								width: 100%;
								height: 100%;
								border-radius: 20rpx;
								background-color: #0d69d1;

								img {
									width: 66%;
									height: 72%;
								}
							}
						}

						.modifyInfo {
							display: flex;
							justify-content: center;
							align-items: center;
							color: #0d69d1;
							font-size: 28rpx;

							.icon {
								position: relative;
								top: 2rpx;
								display: inline-block;
								width: 24rpx;
								height: 24rpx;
								background-image: url("~@/static/userCenter/modify.png");
								background-size: 100% 100%;
								margin-right: 10rpx;
							}
						}
					}
					
				}
			}

			.bodyCard {
				display: flex;
				align-items: center;
				padding: 30rpx 0;

				.item {
					flex: 1;
					display: flex;
					color: #000;
					font-weight: 700;
					justify-content: center;
					align-items: center;
					flex-direction: column;
					height: 80rpx;
					font-size: 26rpx;

					.num {
						margin-bottom: 10rpx;
					}
				}
			}
		}
	}

	.loginBox {
		position: relative;

		.box {
			width: 100%;
			position: absolute;
			left: 0;
			bottom: 50rpx;

			.tips {
				color: red;
				font-size: 24rpx;
				text-align: center;
				margin-bottom: 20rpx;
			}

			.logout {
				color: #fff;
				width: 90%;
				border: none;
				background-color: #0d69d1;
				border-radius: 50rpx;
				font-size: 32rpx;
			}

			.logon {
				color: #fff;
				width: 90%;
				border: none;
				background-color: #0d69d1;
				border-radius: 50rpx;
				font-size: 32rpx;
			}
		}
	}
}
</style>
